#include <fmt/format.h>
#include "../opengl/opengl.hpp"
#include "../gl/Window.hpp"

namespace gl = opengl;

int main() {
    Window window("OpenGL", 800, 600);

    gl::init_opengl(gl::opengl_config{.load_function = &SDL_GL_GetProcAddress,
                                      .debug_msg_callback = [](auto, auto, auto id, auto, auto, auto msg, auto) {
                                          fmt::println("[OpenGL Object {}] {}", id, msg);
                                      }});

    gl::vertex_array vao({
        gl::vertex_attribute(0, gl::type_f32, 3, 0),
        gl::vertex_attribute(1, gl::type_f32, 3, 12),
        gl::vertex_attribute(2, gl::type_f32, 2, 24),
    });
    // postion: vec3, color: vec3, texCoord: vec2
    gl::f32 data[8 * 4] = {
        0.5,  0.5,  0.0, 1.0, 0.0, 0.0, 1.0, 1.0, // Vertex 1
        0.5,  -0.5, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, // Vertex 2
        -0.5, -0.5, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, // Vertex 3
        -0.5, 0.5,  0.0, 1.0, 1.0, 0.0, 0.0, 1.0, // Vertex 4
    };
    gl::u32 index[] = {
        0, 1, 2, // First triangle
        0, 2, 3, // Second triangle
    };
    gl::buffer vertices(sizeof(data), data);
    gl::buffer indices(sizeof(index), index);

    gl::program program({gl::shader::from_file(gl::shader::vertex_shader, "../shader/intro/vs.glsl"),
                         gl::shader::from_file(gl::shader::fragment_shader, "../shader/intro/fs.glsl")});
    fmt::println("{}", program.get_error());

    window.on_render = [&](auto) {
        gl::clear_buffers();
        vao.bind();
        program.use();
        vao.set_vertex_buffer(vertices, 32);
        vao.set_index_buffer(indices);
        gl::api::glDrawElements(gl::triangles, 6, gl::type_u32, nullptr);
    };

    return window.exec();
}